var router = require('koa-router')();

//筹款项目查询
router.post('/raise/approve',async (ctx, next)=>{
    let {
    	statuses='1,2,3',
    	borrower_name='',
    	borrower_phone='',
    	startTime='',
    	endTime='',
    	page=1,
    	size=10
    } = ctx.request.body;


    let count = await ctx.post('/admin/countFundraisingProject',{
			    	statuses,
			    	borrower_name,
			    	borrower_phone,
			    	startTime,
			    	endTime
			    });
    let result = await ctx.post('/admin/listFundraisingProject',{
			    	statuses,
			    	borrower_name,
			    	borrower_phone,
			    	startTime,
			    	endTime,
			    	page,
			    	size
			    });
    if(count.cd==0&&result.cd==0){
    	result.total=count.data;
    }
    
	ctx.body = result;
});

//查询项目信息通过项目ID
router.post('/raise/detail',async (ctx,next) => {
	let {id} = ctx.request.body;
	ctx.body = await ctx.post('/admin/findFundraisingProjectById',{id});
})

//查询投资记录通过项目ID
router.post('/raise/invest' , async (ctx,next) => {
	let {id,page=1,size=10} = ctx.request.body;
	let count = await ctx.post('/admin/countInvestRecordByProjectId',{projectId:id});

	let result = await ctx.post('/admin/findInvestRecordByProjectId',{projectId:id,page,size}); 

	if(count.cd==0&&result.cd==0){
		result.total = count.data
	}
	ctx.body = result;
})

//查询还款记录通过项目ID
router.post('/raise/repayplan' , async (ctx,next) => {
	let {id,page=1,size=100} = ctx.request.body;
	let count = await ctx.post('/admin/countRepayPlanByProjectId',{projectId:id});

	let result = await ctx.post('/admin/findRepayPlanByProjectId',{projectId:id,page,size}); 

	if(count.cd==0&&result.cd==0){
		result.total = count.data
	}
	ctx.body = result;
})

//终止项目
router.post('/raise/stop',async (ctx,next) => {
	let {id} = ctx.request.body;
	ctx.body = await ctx.post('/admin/terminateProject',{projectId:id});
})

//确认放款
router.post('/raise/confirm',async (ctx,next) => {
	let {id} = ctx.request.body;
	ctx.body = await ctx.post('/admin/confirmLoan',{projectId:id});
});

//还款分账
router.post('/raise/splitByAccount' , async (ctx,next) => {
	let {id,period} = ctx.request.body;
	ctx.body = await ctx.post('/admin/splitByAccount',{projectId:id,period});
})

//还款提醒
router.post('/raise/repayTip',async (ctx,next) => {
	let {phone,projectId} = ctx.request.body;
	ctx.body = await ctx.post('/admin/remindRepay',{phone,projectId});
})	

//审批接口
router.post('/raise/loan/approve',async (ctx,next) => {
	let {status,id,check_suggestion=""} = ctx.request.body;
	ctx.body = await ctx.post('/admin/checkProject',{status,project_id:id,check_suggestion});
})

//隐藏项目
router.post('/raise/hide' , async (ctx,next) => {
	let {id,hide_flag} = ctx.request.body;
	ctx.body = await ctx.post('/admin/hideDisplayProject',{id,hide_flag});
})




export default router;
